%{
    #define YYSTYPE float
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
%}

%token NUM
%left '+''-'
%left '*''/'
%right '^'

%%

input:  
        | input line
;

line:       '\n'
        | exp '\n'      { printf("%.2f\n", $1);}
        | '+' '\n'      { yyerror("syntax error");}
        | '-' '\n'      { yyerror("syntax error");}
        | '*' '\n'      { yyerror("syntax error");}
        | '/' '\n'      { yyerror("syntax error");}
        | error '\n'    { yyerrok;} 
;

exp:        NUM         {$$ = $1;}
        | '+' exp exp   {$$ = $2 + $3;}
        | '-' exp exp   {$$ = $2 - $3;}
        | '*' exp exp   {$$ = $2 * $3;}
        | '/' exp exp   {
                        // if ($3 == 0) { 
                        //  yyerror("cannot divide by 0.");
                        //} else {
                            $$ = 1.0 * $2 / $3;
                        //}
                        }
;

%%
yylex() {
    int c;
    fflush(stdin);
    while ((c = getchar()) == ' ' || c == '\t');

    if (isalpha(c)) {
		char msg[50];
		sprintf(msg, "unrecognized token: %c", c);
        yyerror(msg);
    }

    if ((c == '.') || isdigit(c)) {
        ungetc(c, stdin);
        scanf("%f", &yylval);
        return NUM;
    } 
	if (c == EOF) {
        return 0;
    } 
    return c;
}

int main() {
    yyparse();
    return 1;
}

int yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
    return 1;
}

int yywrap() {
    return 1;
}

